import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * Gui class for credit card use
 *
 */

public class CreditCardGui extends JFrame implements ActionListener, FocusListener {

	private static final long serialVersionUID = 1L;
	/** window panel */
	private JPanel jContentPane = null;
	/** number label */
	private JLabel number = null;
	/** company label */
	private JLabel company = null;
	/** expiration date */
	private JLabel exp = null;
	/** ok button - continue to next window after checking validation */
	private JButton ok = null;
	/** cancel - goes to previous window */
	private JButton cancel = null;
	/** company combo box */
	public JComboBox comp = null;
	/** number text field */
	public JTextField num = null;
	/** month text field */
	public JTextField dd = null;
	/** year text field */
	public JTextField mm = null;
	/** slash label */
	private JLabel slash = null;
	/** flag indicating if credit card usage is for buying books or membership */ 
	private boolean flag;
	/**
	 * This is the default constructor
	 */
	public CreditCardGui(boolean flag) {
		super();
		initialize();
		this.flag=flag;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 227);
		this.setContentPane(getJContentPane());
		this.setTitle("Credit Card");
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			slash = new JLabel();
			slash.setBounds(new Rectangle(169, 90, 35, 43));
			slash.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,35));
			slash.setText(" /");
			exp = new JLabel();
			exp.setBounds(new Rectangle(20, 101, 77, 27));
			exp.setText("Expiration");
			company = new JLabel();
			company.setBounds(new Rectangle(19, 65, 78, 24));
			company.setText("Company");
			number = new JLabel();
			number.setText("Number");
			number.setBounds(new Rectangle(19, 26, 67, 22));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(number, BorderLayout.WEST);
			jContentPane.add(company, null);
			jContentPane.add(exp, null);
			jContentPane.add(getOk(), null);
			jContentPane.add(getCancel(), null);
			jContentPane.add(getComp(), null);
			jContentPane.add(getNum(), null);
			jContentPane.add(getDd(), null);
			jContentPane.add(getMm(), null);
			jContentPane.add(slash, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes ok and add listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOk() {
		if (ok == null) {
			ok = new JButton("OK");
			ok.setBounds(new Rectangle(170, 142, 81, 34));
			ok.addActionListener(this);
		}
		return ok;
	}

	/**
	 * This method initializes cancel and add listeners
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton("Cancel");
			cancel.setBounds(new Rectangle(27, 143, 73, 33));
			cancel.addActionListener(this);
		}
		return cancel;
	}

	/**
	 * This method initializes comp	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getComp() {
		if (comp == null) 
		{
			String[] companies = { "MasterCard", "Visa", "Diners"};
			comp = new JComboBox(companies);
			comp.setSelectedIndex(0);
			comp.setBounds(new Rectangle(112, 66, 156, 25));
		}
		return comp;
	}

	/**
	 * This method initializes num	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNum() {
		if (num == null) {
			num = new JTextField();
			num.setBounds(new Rectangle(108, 28, 164, 25));
		}
		return num;
	}

	/**
	 * This method initializes dd and adds listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDd() 
	{
		if (dd == null) {
			dd = new JTextField();
			dd.setBounds(new Rectangle(121, 99, 45, 33));
			dd.setText(" Month"); 
			dd.addFocusListener(this);
		}
		return dd;
	}

	/**
	 * This method initializes mm and adds listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMm() {
		if (mm == null) {
			mm = new JTextField();
			mm.setBounds(new Rectangle(204, 101, 44, 31));
			mm.setText("  Year");
			mm.addFocusListener(this);
		}
		return mm;
	}

	/** if an action is performed on one of the buttons this method is called */
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==getOk())
		{
			Main.creditInfo.company=(String)comp.getSelectedItem();
			try
			{
				Main.creditInfo.number=Integer.parseInt(num.getText());
				Main.creditInfo.month=Integer.parseInt(dd.getText());
				Main.creditInfo.year=Integer.parseInt(mm.getText());
				if((!(Main.creditInfo.month>0 && Main.creditInfo.month<13)) || (!(Main.creditInfo.year>=0 && Main.creditInfo.year<=99)))
					JOptionPane.showMessageDialog(null,"Input Error","ERROR",JOptionPane.ERROR_MESSAGE);
				else
				{
					Main.creditCardControl.validate(flag);
					this.dispose();
				}
			}
			catch(NumberFormatException nFe)
			{
				JOptionPane.showMessageDialog(null,"Input Error","ERROR",JOptionPane.ERROR_MESSAGE); 	
			}
		}
		if(ae.getSource()==getCancel())
			this.dispose();
	}

	/** if month or year is focus the field goes blank */
	public void focusGained(FocusEvent fe) 
	{
		if(fe.getSource()==getDd())
			dd.setText("");
		if(fe.getSource()==getMm())
		mm.setText("");
	
		
	}

	/** implemented method - unused */
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
